//
//  OMPersonalCenterCheapListViewController.m
//  OMENGO
//
//  Created by 梁尚嘉 on 2016/12/20.
//  Copyright © 2016年 Kamfat. All rights reserved.
//

#import "OMPersonalCenterCheapListViewController.h"
#import "OMCouponCell.h"
#import "OMCheapModel.h"
#import "OMCouponSelectTableCell.h"
#import "OMCouponsResponse.h"

@interface OMPersonalCenterCheapListViewController ()

@property (nonatomic, strong) NSMutableArray * cheapListData;
@property (nonatomic, strong) OMCouponsResponse *couponsResponse;
@property (nonatomic, assign) NSUInteger pageNumber;
@end


NSString *const OMCheapSearchPropertyUsing = @"is_used";
//extern NSString *const OMOrderSearchPropertyShipping;
NSString *const OMCheapUsedDate = @"overdate";

@implementation OMPersonalCenterCheapListViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.cheapListData = [NSMutableArray array];
    //分页
    @weakify(self);
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        @strongify(self);
        self.pageNumber++;
        [self getCheapList];
    }];
    footer.stateLabel.font = SM_CFont(24);
    footer.stateLabel.textColor = SM_darkGrayColor;
    footer.triggerAutomaticallyRefreshPercent = SM_SIZE(-40);
    footer.automaticallyHidden = true;
    [footer setTitle:@"没有更多了哦" forState:MJRefreshStateNoMoreData];
    self.tableView.mj_footer = footer;

    self.navigationBarHidden = true;
    self.tableView.rowHeight = [OMCouponSelectTableCell cellHeight];
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SM_SCREEN_WIDTH, SM_SIZE(20))];
    self.emptyImage = [UIImage imageNamed:@"ic_coupons_empty"];
    self.emptyDataMessage = @"哭瞎，木有优惠券";

    [self startAnimating:YES];
    [self getCheapList];

    [self.tableView addRefreshWithBlock:^{
        @strongify(self);
        self.pageNumber = 1;
        [self getCheapList];
    }];
    
}




- (void)setCheapType:(EOMCheapType)cheapType
{
    _cheapType = cheapType;
    switch (cheapType) {
        case EOMCheapUnuseType:
        {
            self.searchValue = EOMSearchValue0;
            self.searchPropety = OMCheapSearchPropertyUsing;
            break;
        }
            
        case EOMCheapExpiredType:
        {
            self.useDate = OMCheapUsedDate;
            break;
            
        }
            
        case EOMCheapUsedType:
        {
            self.searchValue = EOMSearchValue1;
            self.searchPropety = OMCheapSearchPropertyUsing;
            break;
        }
    }
}

- (void)reloadDataFromNetWork
{
    [self getCheapList];
}

- (void)getCheapList
{

    NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithCapacity:2];

    [parameters setObject:@(self.isUsed) forKey:@"isUsed"];
    [parameters  setObject:@(self.pageNumber) forKey:@"pageNumber"];
    [KTHttpUtils GET_NOCACHE:@"member/couponCode/list.jhtml"
                  parameters:parameters
                    progress:nil
                  completion:^(id responseObject, BOOL isSuccess, NSString *message) {
                      if (isSuccess) {
                          self.couponsResponse = [OMCouponsResponse mj_objectWithKeyValues:responseObject[OMDatumKey]];
                          for (OMCheapModel *coupon in self.couponsResponse.list) {
                              coupon.is_used = self.isUsed;
                          }
                          if (self.tableView.mj_footer.isRefreshing) {

                          }else {
                              [_cheapListData removeAllObjects];
                          }
                          [_cheapListData addObjectsFromArray:self.couponsResponse.list];

                          [self.tableView reloadData];
                          [self startAnimating:NO];
                          [self.tableView endRefreshingSuccess];
                          self.couponsResponse.totalPage == self.pageNumber?[self.tableView.mj_footer endRefreshingWithNoMoreData]:[self.tableView.mj_footer endRefreshing];
                      }else {
                          if (self.tableView.mj_footer.isRefreshing) {
                              [self.tableView.mj_footer endRefreshing];
                              self.pageNumber --;
                          }
                          [self showNetWorkError];
                          [self.tableView endRefreshingFail];
                      }
                  }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return _cheapListData.count;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OMCouponSelectTableCell *cell = [OMCouponSelectTableCell tableViewCellWithTableView:tableView];
    [cell configureCellWithObj:_cheapListData[indexPath.section]];
//    [cell configureCellWithType:self.cheapType];
    return cell;
}

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [UIView new];
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return SM_SIZE(20);
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return DBL_EPSILON;
}

- (NSUInteger)pageNumber
{
    if (!_pageNumber) {
        _pageNumber = 1;
    }
    return _pageNumber;
}
@end
